package DesignPattern.Proxy.AddFunction;

/**
 * @Author self
 * @Date 2024/8/12 20:22
 */
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ServiceProxy implements InvocationHandler {
    private Object target;

    public ServiceProxy(Object target) {
        this.target = target;
    }

    // 创建代理实例的方法
    public static Object newProxyInstance(Object target) {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new ServiceProxy(target)
        );
    }

    // 代理类的逻辑实现
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Log: Method " + method.getName() + " is called");
        Object result = method.invoke(target, args);  // 调用实际方法
        System.out.println("Log: Method " + method.getName() + " execution completed");
        return result;
    }
}

